package com.loie.xcx.util;

import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.util.Date;

public
class CopyClass {
    public static void copyFields(Object source, Object destination,String[] arr ) throws Exception {
        Field[] sourceFields = source.getClass().getDeclaredFields();
        Field[] destinationFields = destination.getClass().getDeclaredFields();
        for (String arrStr : arr) {
           // String fieldName = destinationField.getName();
            Field destinationField=destination.getClass().getDeclaredField(arrStr);
            String fieldName = destinationField.getName();

            if (fieldName.substring(fieldName.length()-1).equals("1"))
            {fieldName=fieldName.substring(0,fieldName.length()-1);}
                //System.out.println(fieldName);}
            // 在源类中查找对应的字段
            for (Field sourceField : sourceFields) {
                if (sourceField.getName().equals(fieldName)) {

                    // 如果找到了对应的字段，则进行复制
                    sourceField.setAccessible(true);
                    destinationField.setAccessible(true);
                    if ( StringUtils.isEmpty(sourceField.get(source)))
                    {break;}
                    if (sourceField.getType()==String.class) {
                        String sourceFieldValue = (String) sourceField.get(source);
                        //System.out.println(sourceFieldValue);
                        if (destinationField.getType() == Integer.class) {

                            int destinationFieldValue = Integer.parseInt(sourceFieldValue);
                            destinationField.set(destination, destinationFieldValue);
                        } else if (destinationField.getType() == Double.class) {
                            Double destinationFieldValue = Double.parseDouble(sourceFieldValue);
                            destinationField.set(destination, destinationFieldValue);
                        } else if (destinationField.getType() == Date.class) {
                            Date destinationFieldValue = DateUtil.StringTodatetime(sourceFieldValue);
                            destinationField.set(destination, destinationFieldValue);
                        } else {
                            destinationField.set(destination, sourceFieldValue);
                        }


                    }
                    else
                    {destinationField.set(destination, sourceField.get(source));}
                    break;
                }
            }
        }
    }
//            Field destinationField = destination.getClass().getDeclaredField(sourceField.getName());
//            if (destinationField != null) {
//                sourceField.setAccessible(true);
//                destinationField.setAccessible(true);
//                destinationField.set(destination, sourceField.get(source));
//            }



    public static void copyFieldsStr(Object source, Object destination,String[] souarr,String[] destarr ) throws Exception {
        Field[] sourceFields = source.getClass().getDeclaredFields();
        Field[] destinationFields = destination.getClass().getDeclaredFields();
        for (int i=0;i<=destarr.length-1;i++) {
            String arrStr=destarr[i];
            // String fieldName = destinationField.getName();
            Field destinationField=destination.getClass().getDeclaredField(arrStr);
            String fieldName=souarr[i];
            //System.out.println(fieldName);}
            // 在源类中查找对应的字段
            for (Field sourceField : sourceFields) {
                if (sourceField.getName().equals(fieldName)) {

                    // 如果找到了对应的字段，则进行复制
                    sourceField.setAccessible(true);
                    destinationField.setAccessible(true);
                    if ( StringUtils.isEmpty(sourceField.get(source)))
                    {break;}
                    if (sourceField.getType()==String.class) {
                        String sourceFieldValue = (String) sourceField.get(source);
                        //System.out.println(sourceFieldValue);
                        if (destinationField.getType() == Integer.class) {

                            int destinationFieldValue = Integer.parseInt(sourceFieldValue);
                            destinationField.set(destination, destinationFieldValue);
                        } else if (destinationField.getType() == Double.class) {
                            Double destinationFieldValue = Double.parseDouble(sourceFieldValue);
                            destinationField.set(destination, destinationFieldValue);
                        } else if (destinationField.getType() == Date.class) {
                            Date destinationFieldValue = DateUtil.StringTodatetime(sourceFieldValue);
                            destinationField.set(destination, destinationFieldValue);
                        } else {
                            destinationField.set(destination, sourceFieldValue);
                        }


                    }
                    else
                    {destinationField.set(destination, sourceField.get(source));}
                    break;
                }
            }
        }
    }
        }





